package dhm.com.dhmshop.framework.module.type.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.LazyLoadFragment;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.entity.GetOneCategory;
import dhm.com.dhmshop.framework.module.home.activity.SearchActivity;
import dhm.com.dhmshop.framework.utils.StringUtil;
import io.reactivex.annotations.Nullable;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class ClassIficationFragment extends LazyLoadFragment implements VerticalTabLayout.OnTabSelectedListener, LoginContract.IView {
    @BindView(R.id.mainsearch_type)
    LinearLayout typeMainSearch;
    @BindView(R.id.tab_type)
    VerticalTabLayout typeTab;
    @BindView(R.id.vp_type)
    ViewPager typeVp;
    private View view;

    private List<GetOneCategory.CodeBean> typeEntities;
    private List<Fragment> fragments;
    private ClassIficationVpFgAdapter classificationVpFgAdapter;
    private PressenterImpl pressenter;


    protected void initView() {
        pressenter = new PressenterImpl();
        pressenter.attachView(this);

        typeMainSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                intent.putExtra("type","3");
                startActivity(intent);
            }
        });


        Map<String, String> map = new HashMap<>();
        map.put("token", Constant.TOKEN);
        pressenter.sendMessage(getActivity(), Constant.GetOneCategory, map, GetOneCategory.class);

    }

//    @Override
//    protected void initData() {
//        //调接口发网络请求
//        model.getTabCategory();
//    }

//    @Override
//    protected void initListener() {
//
//    }

//    @Override
//    protected HomeModel initModel() {
//        return new HomeModel();
//    }
//

//    @Override
//    protected int getLayoutId() {
//        return R.layout.fragment_classification;
//    }


/*
    @Override
    public void getCategorySuccess(List<TabCategoryEntity> result) {
        if (result == null) {
            return;
        }
        typeEntities.addAll(result);
        for (TabCategoryEntity categoryEntity : typeEntities){
                fragments.add(new TypeCategoryChildFragment(categoryEntity.getId(),categoryEntity.getName()));

        }

        typeTab.setTabAdapter(new TabAdapter() {
            @Override
            public int getCount() {
                return typeEntities.size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {
                return null;
            }


            @Override
            public QTabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public QTabView.TabTitle getTitle(int position) {
                QTabView.TabTitle title = new QTabView.TabTitle.Builder()
                        .setContent(typeEntities.get(position).getName())//设置数据   也有设置字体颜色的方法
                        .build();
                return title;
            }

            @Override
            public int getBackground(int position) {
                return R.drawable.button_bg;//选中的背景颜色
            }
        });
        classificationVpFgAdapter = new ClassIficationVpFgAdapter(getChildFragmentManager());
        typeVp.setAdapter(classificationVpFgAdapter);
        typeTab.setupWithViewPager(typeVp);
        classificationVpFgAdapter.notifyDataSetChanged();
    }
*/

    @Override
    public void onTabSelected(TabView tab, int position) {
        typeVp.setCurrentItem(position);
    }

    @Override
    public void onTabReselected(TabView tab, int position) {

    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_classification;
    }

    @Override
    protected void lazyLoad() {

    }

    Unbinder unbinder;
    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_classification, container, false);
        }
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    private GetOneCategory getOneCategory;

    @Override
    public void requesta(Object data) {
        if (data instanceof GetOneCategory) {
            getOneCategory = (GetOneCategory) data;

            typeEntities = new ArrayList<>();
            fragments = new ArrayList<>();
            typeEntities.addAll(getOneCategory.getCode());
            for (GetOneCategory.CodeBean categoryEntity : typeEntities) {
                fragments.add(new TypeCategoryChildFragment(categoryEntity.getId(), categoryEntity.getName()));
            }

            typeTab.setTabAdapter(new TabAdapter() {
                @Override
                public int getCount() {
                    return typeEntities.size();
                }

                @Override
                public ITabView.TabBadge getBadge(int position) {
                    return null;
                }


                @Override
                public QTabView.TabIcon getIcon(int position) {
                    return null;
                }

                @Override
                public QTabView.TabTitle getTitle(int position) {
                    QTabView.TabTitle title = new QTabView.TabTitle.Builder()
                            .setContent(typeEntities.get(position).getName())//设置数据   也有设置字体颜色的方法
                            .build();
                    return title;
                }

                @Override
                public int getBackground(int position) {
                    return R.drawable.button_bg;//选中的背景颜色
                }
            });
            classificationVpFgAdapter = new ClassIficationVpFgAdapter(getChildFragmentManager());
            typeVp.setAdapter(classificationVpFgAdapter);
            typeTab.setupWithViewPager(typeVp);
            classificationVpFgAdapter.notifyDataSetChanged();

        }

    }

    @Override
    public void fail(String error) {

    }


    class ClassIficationVpFgAdapter extends FragmentPagerAdapter {

        public ClassIficationVpFgAdapter(FragmentManager fm) {
            super(fm);

        }


        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return StringUtil.preventNull(typeEntities.get(position).getName());
        }
    }
}
